package pos.presentacion;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pos.dominio.Usuario;
import pos.dominio.UsuarioManager;

/**
 * Servlet implementation class FrontController2
 */
public class FrontController2 extends HttpServlet {
	String user = "";
	String passwd = "";
	String nombre="";
	String apellidos="";
	String dni="";
	String email="";
	String dir="";
	String ciudad="";
	private List<Usuario> lista = new LinkedList<Usuario>();
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	public void init() throws ServletException {
	}

    protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
	if(registrar(request)==true){
		String resource = request.getParameter("res");
		RequestDispatcher d = request.getRequestDispatcher(resource);
		if(d!=null){
			d.forward(request,response);
		}
	}else{
		if(valido(request.getParameter("user"),request.getParameter("passwd"))){
			response.sendRedirect("usuarioNoValido.jsp");
		}else{
			response.sendRedirect("errorRegistro.jsp");
		}
	}
	}

	private boolean registrar(HttpServletRequest request){
		boolean registrarse= false;
		HttpSession session=request.getSession(false);

		String userForm = request.getParameter("user");
		String passwdForm = request.getParameter("passwd");
		String nombreForm = request.getParameter("nombre");
		String apForm= request.getParameter("apellidos");
		String dniForm = request.getParameter("dni");
		String emailForm = request.getParameter("email");
		String dirForm = request.getParameter("direccion");
		String ciudadForm = request.getParameter("ciudad");
		session = request.getSession();
			if (userForm == null || passwdForm == null || nombreForm == null || apForm == null || 
					dniForm == null || emailForm == null || dirForm == null || ciudadForm == null
					|| userForm.length() == 0 || passwdForm.length() == 0
					|| nombreForm .length() == 0|| apForm.length() == 0 || 
					dniForm.length() == 0 || emailForm.length() == 0 || dirForm.length() == 0 ||
					ciudadForm.length() == 0) {
				registrarse = false;
			} else {
				if (valido(userForm, passwdForm)) {
					registrarse = false;
					System.out.println("DUPLICADO");
				} else if(!valido(userForm,passwdForm)){
					registrarse = true;
					System.out.println("3");
					session.setAttribute("session.user", userForm);
					session.setAttribute("session.passwd", passwdForm);
					session.setAttribute("session.nombre", nombreForm);
					session.setAttribute("session.apellidos", apForm);
					session.setAttribute("session.dni", dniForm);
					session.setAttribute("session.email", emailForm);
					session.setAttribute("session.direccion", dirForm);
					session.setAttribute("session.ciudad", ciudadForm);
				}
			}
		return registrarse;

	}
	public boolean valido(String userForm, String passForm) {
		boolean res = false;
		lista = UsuarioManager.getInstance().getUsuarios();
		for(Usuario u :lista){
		 user= u.getUsuarioID();
		 passwd=u.getPassword();
		 	
			if((userForm.equals(this.user) && passForm.equals(this.passwd))){
				res=true;
				break;
			}
		}
		return res;

	}
}
